package com.yunlankeji.stemcells.activity.mine;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.db.RecordsDao;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.LogRecord;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

public class Mine_personal_space_searchActivity extends AppCompatActivity {


    ImageView personal_space_clear_all_iv;
    EditText personal_space_query_et;
    TextView personal_space_search_tv, personal_space_history_tv,tv_history;
    LinearLayout cl_toolbar, lt_history_content, personal_space_back_lt;
    TagFlowLayout fl_search_records;
    private List<String> strings ;
    private LayoutInflater mInflater;


    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    fl_search_records.setAdapter(new TagAdapter<String>(strings) {
                        @Override
                        public View getView(FlowLayout parent, int position, String s) {
                            tv_history = (TextView) mInflater.inflate(R.layout.tv_history,
                                    fl_search_records, false);
                            tv_history.setText(s);
                            return tv_history;
                        }
                    });
                    break;
            }
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_personal_space_search);
        personal_space_clear_all_iv = findViewById(R.id.clear_all_records_iv);
        personal_space_back_lt = findViewById(R.id.personal_space_back_lt);
        cl_toolbar = findViewById(R.id.cl_toolbar);
        lt_history_content = findViewById(R.id.lt_history_content);
        personal_space_query_et = findViewById(R.id.personal_space_query_et);
        personal_space_search_tv = findViewById(R.id.personal_space_search_tv);
        personal_space_history_tv = findViewById(R.id.personal_space_history_tv);
        fl_search_records = findViewById(R.id.fl_search_records);
        mInflater = LayoutInflater.from(this);
        fl_search_records = (TagFlowLayout) findViewById(R.id.fl_search_records);
        personal_space_query_et = (EditText) findViewById(R.id.personal_space_query_et);
        personal_space_search_tv = (TextView) findViewById(R.id.personal_space_search_tv);
        strings = new ArrayList<>();
        personal_space_search_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String aa = personal_space_query_et.getText().toString().trim();
                strings.add(aa);
                //通知handler更新UI
                handler.sendEmptyMessageDelayed(1, 0);
            }
        });

        //流式布局tag的点击方法
        fl_search_records.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                Toast.makeText(Mine_personal_space_searchActivity.this, tv_history.getText(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    }
